package com.cabd.web.ajax;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.cabd.domain.User;
import com.cabd.service.UserService;
import com.cabd.service.impl.UserServiceImpl;

public class CheckIdAjax extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String msg = request.getParameter("method");
		if(msg.equals("Login")) {
			login(request, response);
		}else if(msg.equals("register")) {
			register(request, response);
		}
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	void login(HttpServletRequest request, HttpServletResponse response) throws IOException{
		UserService userService = new UserServiceImpl();
		String string = request.getParameter("useridAjax");
		User user = userService.checkId(string);
		if(user == null) {
			String loginErrorInfo = "\"loginAjaxError\":\"用户名不存在!\"";
			response.getWriter().write(loginErrorInfo);
			return;
		}
	}
	
	void register(HttpServletRequest request, HttpServletResponse response) 
			throws IOException {
		UserService userService = new UserServiceImpl();
		String str = request.getParameter("useridAjax");
		User user = userService.checkId(str);
		boolean b = false;
		if(user != null) {
			b=true;
		}
		String registerErrorInfo = "{\"registerAjaxError\":"+b+"}";
		response.setContentType("text/html;charset=UTF-8");
		response.getWriter().write(registerErrorInfo);
	}
}